### Artigo: Por que s�o convocadas as audi�ncias p�blicas no STF?
### Autores: Marjorie Corr�a Marona, Mateus Morais Ara�jo  e Lucas Fernandes de Magalh�es
### C�digos utilizados para gerar os resultados dos gr�ficos 1, 2 e 3,
### bem como dos testes de propor��o do artigo.

library(tidyverse)
library(rio)
library(lubridate)
library(gdata)

##################

rm(list=ls())
options(scipen=999)

setwd() #Definir o caminho da pasta em que se encontram os arquivos baixados

bd <- import("aps_codificada.xlsx")

#########################################################################
#####Grafico descritivo do posicionamento dos participantes x relator####
#########################################################################


g1 <- bd %>% filter(id=="Marona,Araujo,Magalh�es") %>% 
  group_by(nome_ap) %>% 
  mutate(prop=round(Total/sum(Total),2)*100)

g1 <- ggplot(g1, aes(x=nome_ap, y=prop, fill=Alinhamento))+
  geom_col(col="black")+
  geom_text(aes(label=paste0(prop,"%")),color="white",
            position=position_stack(vjust = 0.5))+
  coord_flip()+
  theme_classic()+
  scale_fill_manual("Alinhamento do participante",
                    values=c("grey50","grey30"))+
  labs(y="Propor��o",x="Nome da AP")+
  theme(axis.text.x = element_text(size=14),
        axis.text.y = element_text(size=16),
        axis.title.x = element_text(size=18),
        axis.title.y = element_text(size=18),
        legend.text = element_text(size=18),
        legend.title = element_text(size=18),
  )
ggsave('g1.png',g1,width = 40, height = 20, units = "cm")

####################################
###Teste de hip�tese de propor��o###
####################################


###Base Marona, Ara�jo e Magalh�es
a <- bd %>% filter(id=="Marona,Araujo,Magalh�es") %>% 
  mutate(Alinhamento=ifelse(Alinhamento=="De acordo com o Relator",1,0)) %>% 
  group_by(Alinhamento) %>% 
  summarise(Total=sum(Total))

prop.test(a$Total[a$Alinhamento==1],sum(a$Total))

###Base Leal et al
a <- bd %>% filter(id=="Leal et al") %>% 
  mutate(Alinhamento=ifelse(Alinhamento=="De acordo com o Relator",1,0)) %>% 
  group_by(Alinhamento) %>% 
  summarise(Total=sum(Total))

prop.test(a$Total[a$Alinhamento==1],sum(a$Total))


###Base Falavinha
a <- bd %>% filter(id=="Falavinha") %>% 
  mutate(Alinhamento=ifelse(Alinhamento=="De acordo com o Relator",1,0)) %>% 
  group_by(Alinhamento) %>% 
  summarise(Total=sum(Total))

prop.test(a$Total[a$Alinhamento==1],sum(a$Total))


###########################################
#####SELECAO ENVIESADA DOS ARGUMENTOS######
###########################################

bd <- import("ap_argumentos.xlsx")

a <- bd %>% count(id, alinhamento) %>% 
  group_by(id) %>% 
  mutate(prop=round(n/sum(n),2)*100)

g2 <- ggplot(a, aes(x=id,y=prop,fill=alinhamento))+
  coord_flip()+
  geom_col(col="black")+
  geom_text(aes(label=paste0(prop,"%",
                             " (",n,")")),color="white",
            position=position_stack(vjust = 0.5))+
  theme_classic()+
  scale_fill_manual("Alinhamento do participante",
                    values=c("grey50","grey30"))+
  labs(y="Propor��o",x="Nome da AP")+
  theme(axis.text.x = element_text(size=14),
        axis.text.y = element_text(size=16),
        axis.title.x = element_text(size=18),
        axis.title.y = element_text(size=18),
        legend.text = element_text(size=18),
        legend.title = element_text(size=18),
  )

ggsave('g2.png',g2,width = 40, height = 20, units = "cm")


###Gr�fico total
a <- bd %>% count(tipo, alinhamento) %>% 
  group_by(tipo) %>% 
  mutate(prop=round(n/sum(n),2)*100) %>% 
  mutate(tipo=ifelse(tipo=="relat�rio","Relat�rio",
                     "Fundamenta��o"))

g3 <- ggplot(a, aes(x=tipo, y=prop,fill=alinhamento))+
  geom_col(col="black")+
  theme_classic()+
  geom_text(aes(label=paste0(prop,"%",
                             " (",n,")")),color="white",
            position=position_stack(vjust = 0.5))+
  scale_fill_manual("Alinhamento do participante",
                    values=c("grey50","grey30"))+
  labs(y="Propor��o",x="Parte do voto")+
  theme(axis.text.x = element_text(size=14),
        axis.text.y = element_text(size=16),
        axis.title.x = element_text(size=18),
        axis.title.y = element_text(size=18),
        legend.text = element_text(size=18),
        legend.title = element_text(size=18),
  )


ggsave('g3.png',g3,width = 40, height = 20, units = "cm")

##Prop.teste
prop.test(sum(a$n[a$alinhamento=="De acordo com o Relator"]),
          sum(a$n))
